//
//  SunChangePayCodeVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/31.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunChangePayCodeVC.h"

@interface SunChangePayCodeVC ()<UITextFieldDelegate>
{
    UITextField *_code;
    UITextField *_recode;
    UITextField *_validate;
    UIButton *_timeBtn;
    int _time;
    NSTimer *_timer;
}
@end

@implementation SunChangePayCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND;
    if (!self.title) {
        self.title = @"修改支付密码";
    }
    [self creatUI];
    
    // Do any additional setup after loading the view.
}

- (void)creatUI{
    UIView *view1 = [[UIView alloc]init];
    [self.view addSubview:view1];
    [view1 lt_Top:SunEdgesMake(Y(32), 0, 0, Y(50)) to:nil save:YES];
    view1.backgroundColor = WHITE;
    UIImageView *img1 = [[UIImageView alloc]init];
    [view1 addSubview:img1];
    [img1 lt_YcenterTo:nil save:YES];
    [img1 lt_setLeft:X(15) to:nil save:YES];
    img1.image = [UIImage imageNamed:@"third_getMoney_2"];
    _code = [[UITextField alloc]init];
    [view1 addSubview:_code];
    [_code lt_YcenterTo:nil save:YES];
    [_code lt_setLeft:X(50) to:nil save:YES];
    [_code lt_setWidth:X(300) save:YES];
    _code.textColor = BLACK;
    _code.font = [UIFont systemFontOfSize:17];
    _code.secureTextEntry = YES;
    _code.keyboardType = UIKeyboardTypeNumberPad;
    _code.placeholder = @"请输入新的6为纯数字支付密码";
    _code.delegate = self;
    UIView *view2 = [[UIView alloc]init];
    view2.backgroundColor = WHITE;
    [self.view addSubview:view2];
    [view2 lt_Top:SunEdgesMake(0, 0, 0, Y(50)) to:view1 save:YES];
    UIImageView *img2 = [Sun copyWithView:img1];
    [view2 addSubview:img2];
    [img2 lt_YcenterTo:nil save:YES];
    [img2 lt_setLeft:X(15) to:nil save:YES];
    _recode = [[UITextField alloc]init];
    [view2 addSubview:_recode];
    [_recode lt_YcenterTo:nil save:YES];
    [_recode lt_setLeft:X(50) to:nil save:YES];
    [_recode lt_setWidth:X(300) save:YES];
    _recode.textColor = BLACK;
    _recode.font = [UIFont systemFontOfSize:17];
    _recode.secureTextEntry = YES;
    _recode.keyboardType = UIKeyboardTypeNumberPad;
    _recode.placeholder = @"请输入新的6为纯数字支付密码";
    _recode.delegate = self;
    UIView *view3 = [[UIView alloc]init];
    view3.backgroundColor = WHITE;
    [self.view addSubview:view3];
    [view3 lt_Top:SunEdgesMake(0, 0, 0, Y(50)) to:view2 save:YES];
    UIImageView *img3 = [Sun copyWithView:img1];
    [view3 addSubview:img3];
    [img3 lt_YcenterTo:nil save:YES];
    [img3 lt_setLeft:X(15) to:nil save:YES];
    img3.image = [UIImage imageNamed:@"login_validate"];
    _validate = [[UITextField alloc]init];
    [view3 addSubview:_validate];
    [_validate lt_YcenterTo:nil save:YES];
    [_validate lt_setLeft:X(50) to:nil save:YES];
    [_validate lt_setWidth:X(230) save:YES];
    _validate.textColor = BLACK;
    _validate.font = [UIFont systemFontOfSize:17];
    _validate.keyboardType = UIKeyboardTypeNumberPad;
    _validate.placeholder = @"请输入验证码";
    _validate.delegate = self;
    _timeBtn = [[UIButton alloc]init];
    [view3 addSubview:_timeBtn];
    [_timeBtn lt_YcenterTo:nil save:YES];
    [_timeBtn lt_setRight:X(15) to:nil save:YES];
    _timeBtn.clipsToBounds = YES;
    _timeBtn.layer.cornerRadius = 3;
    [_timeBtn setBackgroundColor:RED forState:UIControlStateNormal];
    [_timeBtn setBackgroundColor:[HXColor colorWithHexString:@"FFB6C1"] forState:UIControlStateHighlighted];
    _timeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    _timeBtn.contentEdgeInsets = UIEdgeInsetsMake(4, 4, 4, 4);
    [_timeBtn setTitleColor:WHITE forState:UIControlStateNormal];
    [_timeBtn setTitle:@"获取验证" forState:UIControlStateNormal];
    [_timeBtn addTarget:self action:@selector(getValidateCode) forControlEvents:UIControlEventTouchUpInside];
    UIButton *next = [[UIButton alloc]init];
    [self.view addSubview:next];
    [next lt_Top:SunEdgesMake(Y(70), X(15), X(15), Y(44)) to:view3 save:YES];
    next.clipsToBounds = YES;
    [next setBackgroundColor:BLUE forState:UIControlStateNormal];
    [next setBackgroundColor:[HXColor colorWithHexString:@"87CEFA"] forState:UIControlStateHighlighted];
    next.layer.cornerRadius = 4;
    [next setTitle:@"确定" forState:UIControlStateNormal];
    [next setTitleColor:WHITE forState:UIControlStateNormal];
    next.titleLabel.font = [UIFont systemFontOfSize:17];
    [next addTarget:self action:@selector(gotoChange) forControlEvents:UIControlEventTouchUpInside];
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = LINE;
    [view1 addSubview:line1];
    [line1 lt_Top:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
    UIView *line2 = [Sun copyWithView:line1];
    [view1 addSubview:line2];
    [line2 lt_Bottom:SunEdgesMake(X(50), 0, 0, 0.5) to:nil save:YES];
    UIView *line3 = [Sun copyWithView:line1];
    [view2 addSubview:line3];
    [line3 lt_Bottom:SunEdgesMake(X(50), 0, 0, 0.5) to:nil save:YES];
    UIView *line4 = [Sun copyWithView:line1];
    [view3 addSubview:line4];
    [line4 lt_Bottom:SunEdgesMake(0, 0, 0, 0.5) to:nil save:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.text.length == 6 && string.length > 0) {
        return NO;
    }
    return YES;
}

- (void)getValidateCode{
    [_timeBtn setTitle:@"发送中" forState:UIControlStateNormal];
    [[Sun shareSun]showActivity:self];
    BOOL HaveAccessID = NO;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
        HaveAccessID = YES;
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,KBase_Send_SMSHttp];
    NSString *parmas = [NSString stringWithFormat:@"phone=%@&sms_type=forget_paypassword",[[NSUserDefaults standardUserDefaults] objectForKey:@"USER_INFO"][@"username"]];
    [HttpRequest postRequestCacheURLStr:url withParameter:parmas HaveAccessID:HaveAccessID IsHTML:NO success:^(NSDictionary *requestDic, NSString *msg) {
        [[Sun shareSun]hideAll];
        [[Sun shareSun]showMsg:@"发送成功"];
        _time = 60;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideAll];
        [[Sun shareSun]showMsg:errorInfo];
        _timeBtn.enabled=YES;
        [_timeBtn setTitle:@"获取验证" forState:UIControlStateNormal];
    }];
}

- (void)onTimer{
    _timeBtn.enabled=NO;
    if(_time == 0){
        _timeBtn.enabled=YES;
        [_timeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [_timer invalidate];
        _timer = nil;
        _time = 60;
        return;
    }
    _time --;
    [_timeBtn setTitle:[NSString stringWithFormat:@"剩余%ds",_time] forState:UIControlStateNormal];
}

- (void)gotoChange{
    if (_code.text.length != 6) {
        [[Sun shareSun]showMsg:@"请输入新的6为纯数字支付密码"];
        return;
    }
    if (_recode.text.length != 6) {
        [[Sun shareSun]showMsg:@"请输入新的6为纯数字支付密码"];
        return;
    }
    if (_validate.text.length != 6) {
        [[Sun shareSun]showMsg:@"请输入新的6为纯数字支付密码"];
        return;
    }
    if (![_code.text isEqualToString:_recode.text]) {
        [[Sun shareSun]showMsg:@"两次输入的密码不相同"];
        return;
    }
    BOOL HaveAccessID = NO;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KLOGIN]) {
        HaveAccessID = YES;
    }
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,KUser_Change_Pay_PWD];
    NSString *parmas = [NSString stringWithFormat:@"password=%@&phone=%@&repassword=%@&valicode=%@",[_code.text stringFromUTF8],[[NSUserDefaults standardUserDefaults] objectForKey:@"USER_INFO"][@"username"],[_recode.text stringFromUTF8],_validate.text];
    [HttpRequest postRequestCacheURLStr:url withParameter:parmas HaveAccessID:HaveAccessID IsHTML:NO success:^(NSDictionary *requestDic, NSString *msg) {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"paypassword"];
        [[StDataShare ShareAlertView]show:msg];
        if (self.gotoNext) {
            [self.navigationController popToViewController:self.navigationController.childViewControllers[self.navigationController.childViewControllers.count - 3] animated:YES];
        }else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSString *errorInfo, int code) {
        [[StDataShare ShareAlertView]show:errorInfo];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
